﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LaKuaShop_TestDemo
{
    /// <summary>
    /// ChangeInfo.xaml 的交互逻辑
    /// </summary>
    public partial class ChangeInfo : Window
    {
        public ChangeInfo()
        {
            InitializeComponent();
            InitUser();
        }
        private void InitUser()
        {
            newAccount.Text = Common.User.account;
            Male.IsChecked = Common.User.sex == "男";
            Famale.IsChecked = Common.User.sex == "女";
        }
        private void Close_Click(object sender, RoutedEventArgs e)
        {

            this.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string account = newAccount.Text;
            string oldact = Common.User.account;
            string sex = (Male.IsChecked == true) ? "男" : "女";
            if (account == "" || (Male.IsChecked == false && Famale.IsChecked == false))
            {
                ExistMsg.IsActive = false;
                ERRORMsg.IsActive = false;
                SnackbarsavedChanges.IsActive = false;
                NullMsg.IsActive = true;
                return;
            }
            BLL.User User = new BLL.User();
            if (newAccount.Text != Common.User.account)//修改了账户
            {
                if (User.CheckAct(account) == false)//账户名已被注册
                {
                    NullMsg.IsActive = false;
                    ERRORMsg.IsActive = false;
                    ExistMsg.IsActive = true;
                    SnackbarsavedChanges.IsActive = false;
                    return;
                }
                else
                {
                    NullMsg.IsActive = false;
                    ERRORMsg.IsActive = false;
                    ExistMsg.IsActive = false;
                    SnackbarsavedChanges.IsActive = true;
                }
            }
            else if (sex != Common.User.sex)//仅仅修改了性别
            {
                Common.User.sex = sex;
                if (User.UpdateUserInfo(Common.User, oldact) == 0)
                {
                    Common.User = User.ShowUserInfo(account);
                    this.Close();
                }
            }
        }

        private void save_Akg_Click(object sender, RoutedEventArgs e)
        {
            string account = newAccount.Text;
            string oldact = Common.User.account;
            string sex = (Male.IsChecked == true) ? "男" : "女";
            Common.User.account = newAccount.Text;
            Common.User.sex = sex;
            BLL.User User = new BLL.User();
            BLL.Order Order = new BLL.Order();
            BLL.Collect Collect = new BLL.Collect();
            BLL.BuyCart BuyCart = new BLL.BuyCart();
            if ((User.UpdateUserInfo(Common.User, oldact) == 0) && (Order.UpdOrderAct(newAccount.Text, oldact) == 0) && (Collect.UpdColAct(newAccount.Text, oldact) == 0) && (BuyCart.UpdBuyCartAct(newAccount.Text, oldact) == 0) && (User.UpdUserIconAct(newAccount.Text, oldact) == 0))
            {
                Common.User = User.ShowUserInfo(account);
                this.Close();
            }
            else
            {
                NullMsg.IsActive = false;
                ERRORMsg.IsActive = true;
                ExistMsg.IsActive = false;
                SnackbarsavedChanges.IsActive = false;
            }
        }
    }
}
